﻿// JScript 文件 联动选择
var DdSelectMenu =  function(appendId,appendId2, selectId, selectId2, url) {
	
	var appendId = appendId;
	var appendId2 = appendId2;
	
	var selectId = selectId;
	var selectId2 = selectId2;
	var url  = url;	


	this.subChange = subChange;
    this.respCity = respCity;
    this.respProvince = respProvince;
    this.renderSelect = renderSelect;
    
    this.loadData = loadData;

//Change 事件
function subChange ()
{                     									
			var cityId = $(selectId).value;								
			loadData("city",cityId)
}
//画出Select
function renderSelect(id,appendId, originalRequest)
{
            var tex = originalRequest.responseText;	 
            
            if(tex == "") return;
			var subdateObj = tex.evalJSON();
								
			var selectObj = document.createElement("select");			
			selectObj.setAttribute("id",id);	
			selectObj.setAttribute("name",id);
			
			var len = subdateObj.List.length	
					
			for(var i=0 ;i < len ;i ++)
			{			
			     selectObj.options.add(new Option(subdateObj.List[i].Name,subdateObj.List[i].Id));		
			}
		
			if($(id) != null) 
			{			
			    $(appendId).replaceChild(selectObj,$(id));		
			}
			else
			{																				
				$(appendId).appendChild(selectObj);							
			}
		
}

//生成城市的Select
function respCity(originalRequest){ 
           
        renderSelect(selectId2,appendId2, originalRequest);                   
}
//生成省的Select
function respProvince(originalRequest){
              	 			 						
		renderSelect(selectId,appendId, originalRequest)						
		Event.observe($(selectId), 'change', subChange, false);		
}
    
//回调数据
function loadData(loadType,cityId)
{

    var cityId = (arguments.length>1)?arguments[1]:"";             
    var respFunction = (loadType != "city")? respProvince: respCity ;

    var parsStr = "?Dtype="+ loadType + "&Id=" + cityId ;//+ "&d="  + new Date();
    var myurl = url + parsStr ;
         		    
    var pars = "";	   	  
    var myAjax = new  Ajax.Request(myurl,{method: 'get', parameters: pars, onComplete: respFunction});
						
    }
     		
};